/*日期输入控件*/
!function($){
    //日期类
    var DateInput = function(element, options) {
        this.element = $(element);
        this.maxyear = options.maxyear;
        this.datebox = $("<div class='dateinput_box'></div>");
        this.year_box = $("<div class='year_box year_value_bg'></div>");
        this.month_box = $("<div class='month_box'></div>");
        this.day_box = $("<div class='day_box'></div>");
        this.year_value = $("<input class='year_value year_value_bg' maxlength='10' value='"+options.yearstr+"'/>");
        this.month_value = $("<input class='month_value month_value_bg' maxlength='4' value='"+options.monthstr+"'/>");
        this.day_value = $("<input class='day_value day_value_bg' maxlength='4' value='"+options.daystr+"'/>");
        this.year_ul = $("<ul class='year_ul hide'></ul>");
        this.month_ul = $("<ul class='month_ul hide'></ul>");
        this.day_ul = $("<ul class='day_ul hide'></ul>");
    };

    function _yearBlur(year_value, year_ul, maxy) {
        $(year_value).attr("class","year_value year_value_bg");
        $(year_ul).addClass("hide");
        var yvalue = $(year_value).val();
        var year = new Date().getFullYear();
        for(var i=year+maxy;i>year-100;--i) {
            if(yvalue==i) {
                $(year_value).val(yvalue);
                return;
            }
        }
        $(year_value).val(year);
    }
    function _monthBlur(month_value, month_ul) {
        $(month_value).attr("class", "month_value month_value_bg");
        $(month_ul).addClass("hide");
        var mvalue = $(month_value).val();
        for(var i=1;i<13;++i) {
            if(mvalue==i) {
                $(month_value).val(mvalue);
                return;
            }
        }
        $(month_value).val("01");
    }
    function _dayBlur(year_value, month_value, day_value, day_ul) {
        $(day_value).attr("class", "day_value day_value_bg");
        $(day_ul).addClass("hide");
        var dvalue = $(day_value).val();
        var year = $(year_value).val();
        var month = $(month_value).val();
        if(year!=""&&month!="") {
            var days = new Date(year,month,0).getDate();
            for(var i=1;i<=days;++i) {
                if(dvalue==i) {
                    $(day_value).val(dvalue);
                    return;
                }
            }
        }
        if(/^[0-9]*[1-9][0-9]*$/.test(year)&&/^[0-9]*[1-9][0-9]*$/.test(month)) {
            $(day_value).val("01");
        }else {
            $(day_value).val("");
        }
    }

    function _initDay(year_val, month_val, day_ul) {
        $(day_ul).html("");
        var year = $(year_val).val();
        var month = $(month_val).val();
        if(year!=""&&month!="") {
            var days = new Date(year,month,0).getDate();
            var dayHtml = ""
            for(var i=1;i<=days;++i) {
                dayHtml += "<li name='day_" + i + "' value='" + i + "'>" + (i<10 ? "0"+i : i) + "</li>";
            }
            $(day_ul).html(dayHtml);
        }
    }

    DateInput.prototype = {
        _init : function() {
            this.element.after(this.datebox);
            this.datebox.append(this.year_box);
            this.datebox.append(this.month_box);
            this.datebox.append(this.day_box);
            this.year_box.append(this.year_value);
            this.year_box.append(this.year_ul);
            this.month_box.append(this.month_value);
            this.month_box.append(this.month_ul);
            this.day_box.append(this.day_value);
            this.day_box.append(this.day_ul);

            var year = new Date().getFullYear();
            var yearHtml = ""
            for(var i=year+this.maxyear;i>year-100;--i) {
                yearHtml += "<li name='year_" + i + "' value='" + i + "'>" + i + "</li>";
            }
            $(this.year_ul).html(yearHtml);
            var monthHtml = ""
            for(var i=1;i<13;++i) {
                monthHtml += "<li name='month_" + i + "' value='" + i + "'>" + (i<10 ? "0"+i : i) + "</li>";
            }
            $(this.month_ul).html(monthHtml);
        },
        _bindEvent : function() {
            var y_val = this.year_value;
            var y_ul = this.year_ul;
            var m_val = this.month_value;
            var m_ul = this.month_ul;
            var d_val = this.day_value;
            var d_ul = this.day_ul;
            var maxy = this.maxyear;

            $(y_val).bind({
                focus : function() {
                    y_val.attr("class", "year_value year_value_focus");
                    $(y_ul).removeClass("hide");
                    if($.browser.webkit) {
                    }else {
                        $(y_val).select();
                    }
                },
                click : function() {
                    if($.browser.webkit) {
                        $(y_val).select();
                    }
                },
                blur : function() {
                    _yearBlur(y_val, y_ul, maxy);
                },
                keypress : function(e) {
                    var e = e || window.event;
                    if(e.keyCode == 13){
                        $(y_val).blur();
                    }
                },
                keydown : function(e) {
                    var e = e || window.event;
                    if(e.keyCode == 13){
                        $(y_val).blur();
                    }
                }
            });
            $(m_val).bind({
                focus : function() {
                    m_val.attr("class", "month_value month_value_focus");
                    $(m_ul).removeClass("hide");
                    if($.browser.webkit) {
                    }else {
                        $(m_val).select();
                    }
                },
                click : function() {
                    if($.browser.webkit) {
                        $(m_val).select();
                    }
                },
                blur : function() {
                    _monthBlur(m_val, m_ul);
                },
                keypress : function(e) {
                    var e = e || window.event;
                    if(e.keyCode == 13){
                        $(m_val).blur();
                    }
                },
                keydown : function(e) {
                    var e = e || window.event;
                    if(e.keyCode == 13){
                        $(m_val).blur();
                    }
                }
            });
            $(d_val).bind({
                focus : function() {
                    d_val.attr("class", "day_value day_value_focus");
                    $(d_ul).removeClass("hide");
                    _initDay(y_val, m_val, d_ul);

                    $(d_ul).bind({
                        mouseover : function() {
                            $(d_val).unbind("blur");
                        },
                        mouseout : function() {
                            $(d_val).bind("blur",function() {
                                _dayBlur(y_val, m_val, d_val, d_ul);
                            });
                        }
                    });
                    $(d_ul).find("li").bind({
                        mouseover : function() {
                            $(this).addClass("hover");
                        },
                        mouseout : function() {
                            $(this).removeClass("hover");
                        },
                        click : function() {
                            $(d_val).val($(this).text());
                            $(d_val).attr("class","day_value day_value_bg");
                            $(d_ul).addClass("hide");
                        }
                    });
                    if($.browser.webkit) {
                    }else {
                        $(d_val).select();
                    }
                },
                click : function() {
                    if($.browser.webkit) {
                        $(d_val).select();
                    }
                },
                blur : function() {
                    _dayBlur(y_val, m_val, d_val, d_ul);
                },
                keypress : function(e) {
                    var e = e || window.event;
                    if(e.keyCode == 13){
                        $(d_val).blur();
                    }
                },
                keydown : function(e) {
                    var e = e || window.event;
                    if(e.keyCode == 13){
                        $(d_val).blur();
                    }
                }
            });

            $(y_ul).bind({
                mouseover : function() {
                    $(y_val).unbind("blur");
                },
                mouseout : function() {
                    $(y_val).bind("blur",function() {
                        _yearBlur(y_val, y_ul, maxy);
                    });
                }
            });
            $(y_ul).find("li").bind({
                mouseover : function() {
                    $(this).addClass("hover");
                },
                mouseout : function() {
                    $(this).removeClass("hover");
                },
                click : function() {
                    $(y_val).val($(this).text());
                    $(y_val).attr("class","year_value year_value_bg");
                    $(y_ul).addClass("hide");
                }
            });

            $(m_ul).bind({
                mouseover : function() {
                    $(m_val).unbind("blur");
                },
                mouseout : function() {
                    $(m_val).bind("blur",function() {
                        _monthBlur(m_val, m_ul);
                    });
                }
            });
            $(m_ul).find("li").bind({
                mouseover : function() {
                    $(this).addClass("hover");
                },
                mouseout : function() {
                    $(this).removeClass("hover");
                },
                click : function() {
                    $(m_val).val($(this).text());
                    $(m_val).attr("class","month_value month_value_bg");
                    $(m_ul).addClass("hide");
                }
            });
        }

    };

    $.fn.dateinput = function(options) {
        var settings = {
            languagetype : "0",
            yearstr : "年",
            monthstr : "月",
            daystr : "日",
            maxyear : ""
        };

        if(options) {
            $.extend(settings, options);
        };
        var date = this.val();
        if(date==null||date=="null"||date=="") {
            if(settings.languagetype=="0"){
                settings.yearstr = "年";
                settings.monthstr = "月";
                settings.daystr = "日";
            }else if(settings.languagetype=="1"){
                settings.yearstr = "Y";
                settings.monthstr = "M";
                settings.daystr = "D";
            }else{
                settings.yearstr = "年";
                settings.monthstr = "月";
                settings.daystr = "日";
            }
        }else {
            var ymd = [settings.yearstr, settings.monthstr, settings.daystr];
            if(date.indexOf("-")>0) {
                ymd = this.val().split("-");
            }else if(date.indexOf("/")>0) {
                ymd = this.val().split("/");
            }
            settings.yearstr = ymd[0];
            settings.monthstr = ymd[1];
            settings.daystr = ymd[2];
        }
        if(settings.maxyear&&settings.maxyear!=""&&/^-?\d+$/.test(settings.maxyear)) {
            settings.maxyear = parseInt(settings.maxyear);
        }else {
            settings.maxyear = 0;
        }

        return this.each(function() {
            var dateipt = new DateInput(this, settings);
            dateipt._init();//初始化控件
            dateipt._bindEvent();//绑定各种事件
        });
    };
}(jQuery);